{extend name="public/base"}

{block name="x-body"}
    <div class="x-body">
      <blockquote class="layui-elem-quote">
        <span style="color: red;">提示：上架的商品不允许被更改</span>
      </blockquote>
      <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so" method="get">
          <div class="layui-input-inline">
            <select name="cate_id">
              <option value="">全部分类</option>
              {volist name="goods_cate" id="v"}
              <option value="{$v.id}" {$search_key.cate_id == $v.id ? 'selected' : ''}>{$v.cate_name}</option>
              {/volist}
            </select>
          </div>
          <div class="layui-input-inline">
            <select name="is_on_sale">
              <option value="">全部商品</option>
              <option value="1" {$search_key.is_on_sale == 1 ? 'selected' : ''}>上架</option>
              <option value="0" {$search_key.is_on_sale === '0' ? 'selected' : ''}>下架</option>
            </select>
          </div>
          <div class="layui-input-inline">
            <select name="exchange_integral">
              <option value="">商品类型</option>
              <option value="=" {$search_key.exchange_integral == '=' ? 'selected' : ''}>普通商品</option>
              <option value="<>" {$search_key.exchange_integral === '<>' ? 'selected' : ''}>积分商品</option>
            </select>
          </div>
          <input type="text" name="goods_name"  placeholder="请输入搜索词" autocomplete="off" class="layui-input" value="{$search_key.goods_name}">
          <button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
        </form>
      </div>
      <xblock>
        <button class="layui-btn layui-btn-danger" onclick="delAll('{:url(\'goods/goods_del\')}')"><i class="layui-icon"></i>批量删除</button>
        <button class="layui-btn" onclick="x_admin_show('添加商品','{:url(\'goods/goods_form\')}')"><i class="layui-icon"></i>添加商品</button>
        <span class="x-right" style="line-height:40px">共有数据：{$list -> total()} 条</span>
      </xblock>
      <table class="layui-table layui-form">
        <thead>
          <tr>
            <th>
              <div class="layui-unselect header layui-form-checkbox" lay-skin="primary"><i class="layui-icon">&#xe605;</i></div>
            </th>
            <th>id</th>
            <th>商品编号</th>
            <th>商品名称</th>
            <th>所属分类</th>
            <th>价格</th>
            <th>库存</th>
            <th>排序</th>
            <th>推荐</th>
            <th>上/下架</th>
            <th >操作</th>
            </tr>
        </thead>
        <tbody>
          {volist name="list" id="v"}
          <tr>
            <td>
              <div class="layui-unselect layui-form-checkbox" lay-skin="primary" data-id='{$v.id}'><i class="layui-icon">&#xe605;</i></div>
            </td>
            <td>{$v.id}</td>
            <td>{$v.goods_sn}</td>
            <td>{$v.goods_name}</td>
            <td>{$v.cate_name}</td>
            <td>
              {if $v.exchange_integral}
                <img src="__STATIC__/common/images/integral.png" width="20" alt=""> {$v.exchange_integral}
              {else /}
                <img src="__STATIC__/common/images/money.png" width="20" alt=""> {$v.shop_price}
              {/if}
            </td>
            <td>{$v.store_count}</td>
            <td style="width: 5%;"><input type="text" class="layui-input x-sort" name="sort" data-id="{$v.id}" value="{$v.sort}" onfocus="isNum(this)"></td>
            <td style="width: 5%;">
              <input type="checkbox" name="is_recommend" data-id="{$v.id}" data-status="{$v.is_recommend}" lay-text="是|否" {if $v.is_recommend == 1}checked=""{/if} lay-skin="switch" >
            </td>
            <td style="width: 5%;">
              <input type="checkbox" name="is_on_sale" data-id="{$v.id}" data-status="{$v.is_on_sale}" lay-text="是|否" {if $v.is_on_sale == 1}checked=""{/if} lay-skin="switch" >
            </td>
            <td class="td-manage">
              <a title="查看"  onclick="x_admin_show('编辑商品','{:url(\'goods/goods_form\', [\'id\' => $v.id])}')" href="javascript:;">
                <i class="layui-icon">&#xe63c;</i>
              </a>
              <a title="删除" onclick="member_del(this,'{$v.id}', '{:url(\'goods/goods_del\')}')" href="javascript:;">
                <i class="layui-icon">&#xe640;</i>
              </a>
            </td>
          </tr>
          {/volist}
        </tbody>
      </table>
      <div class="page">
        {$list -> render()|raw}
      </div>

    </div>
<script>
    $(document).ready(function (){
        /**
         * 更新排序
         */
        sort('{:url("goods/goodsSort")}');

        var url = '{:url("goods/goodsStatus")}';

        layui.use('form', function (){
            var form = layui.form;
            /**
             * 更新状态
             */
            $('table').on('click', '.layui-form-switch', function (){
                var id = $(this).prev().data('id');
                var status_name = $(this).prev().attr('name');
                var that = $(this).prev();
                var status = that.data('status') == 0 ? 1 : 0;
                var data = {'id': id};
                data[status_name] = status;
                $.post(url, data, function(res){
                    if(res.code == 1){
                        that.data('status', status);
                    }else{
                        var check = $(that).prop('checked');
                        $(that).prop('checked', !check);
                        form.render('checkbox');
                    }

                    layer.msg(res.msg, {icon:1,time:1000});
                }, 'json');
            })
        })
    });
</script>
{/block}